/**
 * @file index.js
 * @author swan
 */
const app = getApp()
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var bdApi = require('../../utils/bdApi.js');
var bdRequest = require('../../utils/bdRequest.js')
import config from '../../utils/config.js'
var pageCount = config.getPageCount;
var WxParse = require('../../wxParse/wxParse.js');
import seoConfig from '../../seoConfig/config.js'
Page({
    data: {
        title: '文章列表',
        postsList: {},
        pagesList: {},
        categoriesList: {},
        postsShowSwiperList: {},
        isLastPage: false,
        page: 1,
        search: '',
        categories: 0,
        specialName:'',
        categoriesImage:"", 
        showerror:"none",
        isCategoryPage:"none",
        isSearchPage:"none",
        showallDisplay: "block",
        displaySwiper: "block",
        floatDisplay: "none",
        searchKey:"",
        description:'',
        special:0
    },
    //加载分页
  loadMore: function (e) {
    var self = this;
    if (!self.data.isLastPage) {
      self.setData({
        page: self.data.page + 1
      });
      console.log('当前页' + self.data.page);
      this.fetchPostsData(self.data);
    }
    else {
      swan.showToast({
        title: '没有更多内容',
        mask: false,
        duration: 1000
      });
    }
  },
  onLoad: function (options) {
    var self = this;
    
    this.getSpecial(options.id)
  },
  getSpecial(id){
  var getSpecialRequest = bdRequest.getRequest(Api.getSpecial(id));
    getSpecialRequest.then(response =>{
        if (response.statusCode === 200) {
      

          this.setData({
              specialName: response.data.name,
              description:response.data.description,
              special:id
          }) 
          swan.setNavigationBarTitle({
              title: response.data.name,
          });
           swan.setPageInfo({
                        title:response.data.name + '-办得爽',
                        keywords:response.data.name,
                        description:response.data.description,
                        success: function () {
                                console.log('页面基础信息设置完成');
                        }
                    })
          this.fetchPostsData()
        }
    })
  },
  //获取文章列表数据
  fetchPostsData: function (data) {
    var self = this;  
    if (!data) data = {};
    if (!data.page) data.page = 1;
    if (!data.special) data.special = this.data.special;
    if (data.page === 1) {
      self.setData({
        postsList: []
      });
    };
    
    swan.showLoading({
      title: '正在加载',
      mask:true
    });

    var getPostsRequest = bdRequest.getRequest(Api.getSpecialPostListByPage(data));
    getPostsRequest.then(response =>{
        if (response.statusCode === 200) {
            // console.log(response.data)
          for (let i = 0; i < response.data.length; i++) {
            let rendered = response.data[i].excerpt.rendered;
            WxParse.bdParse('rendered', 'html', rendered, this, 5)
            
            response.data[i].excerpt.rendered = this.data.rendered;
          }
        //  let urls=[]
        //  for(let item of response.data){
        //      var getSpecialImg = bdRequest.getRequest(Api.getSpecailImg(item.featured_media));  
        //      urls.push(getSpecialImg)
        //  }
         
        //  Promise.all(urls).then((result) => {
        //      console.log(result)
        //     for(let i=0;i<result.length;i++){
        //         response.data[i].post_thumbnail_image=result[i].data.source_url
        //     }
             self.setData({
                floatDisplay: "block",
                showallDisplay: "block",
                postsList: self.data.postsList.concat(response.data.map(function (item) {
                    var strdate = item.date
                    item.date = util.cutstr(strdate, 10, 1);
                    return item;
                }))

          });
        // }).catch((error) => {
        
        // })
            if (response.data.length < pageCount) {
                self.setData({
                    isLastPage: true
                });
            };
        }
        else {
            if (response.data.code == "rest_post_invalid_page_number") {
                self.setData({
                    isLastPage: true
                });

            }
            else {
                swan.showToast({
                    title: response.data.message,
                    duration: 1500
                })
            }
        }   

    })
    .catch(function(){        
        if (data.page == 1) {

            self.setData({
                showerror: "block",
                floatDisplay: "none"
            });

        }
        else {
            swan.showModal({
                title: '加载失败',
                content: '加载数据失败,请重试.',
                showCancel: false,
            });
            self.setData({
                page: data.page - 1
            });
        }
    })
        .finally(function () {
            swan.hideLoading();

        })  
  },  



  // 跳转至查看文章详情
  redictDetail: function (e) {
    // console.log('查看文章');
    var id = e.currentTarget.id,
      url = '../detail/detail?id=' + id;
    swan.navigateTo({
      url: url
    })
  }
})
